
class MyThread extends Thread {
    @Override
    public void run() {
        while (true) {
            System.out.println("hello Thread!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

public class TestDemo {

    public static final long COUNT = 20_0000_0000;

    public static void serial() {
        long beg = System.currentTimeMillis();

        int a = 0;
        for (long i = 0; i < COUNT; i++) {
            a++;
        }

        a = 0;
        for (long i = 0; i < COUNT; i++) {
            a++;
        }

        long end = System.currentTimeMillis();
        System.out.println("单线程总耗时:" + (end - beg) + "ms");
    }

    public static void concurrency() {
        long beg = System.currentTimeMillis();

        Thread t1 = new Thread(() -> {
            int a = 0;
            for (long i = 0; i < COUNT; i++) {
                a++;
            }
        });

        Thread t2 = new Thread(() -> {
            int a = 0;
            for (long i = 0; i < COUNT; i++) {
                a++;
            }
        });

        t1.start();
        t2.start();

        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        long end = System.currentTimeMillis();
        System.out.println("多线程总耗时:" + (end - beg) + "ms");
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            System.out.println("haha");
        });
        t1.start();

        System.out.println(t1.isDaemon());
    }
    public static void main2(String[] args) {
        serial();
        concurrency();
    }

    public static void main1(String[] args) {
        MyThread myThread = new MyThread();
        myThread.start();

        while (true) {
            System.out.println("hello main!");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
